/*******************************************************************************
 * @file mbuf.h
 * @brief:
 *
 *
 * Author: Kevinx
 *
 * Copyright (c) 2015 Kevinx. All Rights Reserved.
 *
 * CONFIDENTIALITY AND LIMITED USE
 *
 * This software, including any software of third parties embodied herein,
 * contains information and concepts which are confidential to Kevinx
 * and such third parties. This software is licensed for use
 * solely in accordance with the terms and conditions of the applicable
 * license agreement with Kevinx or its authorized distributor.
 ************************************************************************************/
#ifndef __MEM_BUF_H_
#define __MEM_BUF_H_
#include <stdint.h>
#include "cyclic_list.h"

typedef char* caddr_t;

struct mhdr {
    uint32_t len;
    int flags;
    int id;
    struct fdt_channel* in_ch;
    struct list_head list;   
};

#define PAGE_SIZE 4096
#define M_ORDER 11
#define M_SIZE (1 << M_ORDER) /*2048 bytes*/
#define M_HDR_SZ 128
#define M_BACK_SZ 128
#define M_DATA_SZ (M_SIZE - M_HDR_SZ - M_BACK_SZ)

#define MF_GARBAGE 0x01

struct mbuf {
    struct {
    	union {
    		struct mhdr hdr;
        	char mh_data[M_HDR_SZ];
    	} u;

    	char data[M_BACK_SZ];
    } s;

    char databuf[M_DATA_SZ];
};

#define m_list s.u.hdr.list

#define mtod(m, t) ((t)((m)->s.data))
#define mtodbuf(m, t) ((t)((m)->databuf))

#define dtom(p) ((struct mbuf*)((unsigned long)(p) & ~(M_SIZE - 1)))

#define m_hdr(m) ((struct mhdr*)((m)->s.u.mh_data))

#define m_init(m) \
    do { \
    	m_hdr(m)->len = 0; \
    	m_hdr(m)->flags = 0; \
    	m_hdr(m)->in_ch = NULL; \
    	m_hdr(m)->list.prev = NULL; \
    	m_hdr(m)->list.next = NULL; \
       } while (0)


#endif /* __MEM_BUF_H_*/
